package com.stationbuilding.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.stationbuilding.R;
import com.stationbuilding.TestData;
import com.stationbuilding.activity.member.MemberDetailsActivity;
import com.stationbuilding.activity.message.MessageActivity;
import com.stationbuilding.activity.progress.ProgressAddActivity;
import com.stationbuilding.activity.quality.QualityAddActivity;
import com.stationbuilding.activity.safe.SafeAddActivity;
import com.stationbuilding.activity.settings.SettingsActivity;
import com.stationbuilding.adapter.project.ProjectListAdapter;
import com.stationbuilding.dialog.FullScreenDialog;
import com.stationbuilding.fragment.task.TaskFragment;
import com.stationbuilding.fragment.document.DocumentFragment;
import com.stationbuilding.fragment.home.HomeFragment;
import com.stationbuilding.fragment.member.MemberFragment;
import com.stationbuilding.utils.AlarmManagerUtils;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment = new HomeFragment();
    private DocumentFragment documentFragment = new DocumentFragment();
    private TaskFragment taskFragment = new TaskFragment();
    private MemberFragment memberFragment = new MemberFragment(false);
    private Fragment currentFragment;
    private FullScreenDialog fullScreenDialog;
    private PopupWindow window;

    // 点击事件监听监听
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.menu:
                    initPopupWindow();
                    break;
                case R.id.bell:
                    startActivity(new Intent(MainActivity.this, MessageActivity.class));
                    break;
                case R.id.safe_img:
                case R.id.safe:
                    startActivity(new Intent(MainActivity.this, SafeAddActivity.class));
                    fullScreenDialog.dismiss();
                    break;
                case R.id.quality_img:
                case R.id.quality:
                    startActivity(new Intent(MainActivity.this, QualityAddActivity.class));
                    fullScreenDialog.dismiss();
                    break;
                case R.id.progress_img:
                case R.id.progress:
                    startActivity(new Intent(MainActivity.this, ProgressAddActivity.class));
                    fullScreenDialog.dismiss();
                    break;
                case R.id.close:
                    fullScreenDialog.dismiss();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // navigation监听
        BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        // 首页
                        switchFragment(homeFragment);
                        return true;
                    case R.id.navigation_document:
                        // 文档
                        switchFragment(documentFragment);
                        return true;
                    case R.id.navigation_add:
                        // 事件
                        showSelectDialog();
                        return false;
                    case R.id.navigation_task:
                        // 待办
                        switchFragment(taskFragment);
                        return true;
                    case R.id.navigation_member:
                        // 成员
                        switchFragment(memberFragment);
                        return true;
                }
                return false;
            }
        };
        fullScreenDialog = new FullScreenDialog(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView menu = (ImageView) toolbar.findViewById(R.id.menu);
        ImageView bell = (ImageView) toolbar.findViewById(R.id.bell);
        menu.setOnClickListener(onClickListener);
        bell.setOnClickListener(onClickListener);
        // 初始化NavigationBar
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
        disableShiftMode(navigation);
        navigation.setSelectedItemId(R.id.navigation_home);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @SuppressLint("RestrictedApi")
    private void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                item.setShiftingMode(false);
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private void switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            // 第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.content, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction.hide(currentFragment).show(targetFragment);
        }
        currentFragment = targetFragment;
        transaction.commitAllowingStateLoss();
    }

    private void showSelectDialog() {
        RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.dialog_select, null);
        ImageView close = (ImageView) layout.findViewById(R.id.close);
        ImageView safeImg = (ImageView) layout.findViewById(R.id.safe_img);
        ImageView qualityImg = (ImageView) layout.findViewById(R.id.quality_img);
        ImageView progressImg = (ImageView) layout.findViewById(R.id.progress_img);
        TextView safe = (TextView) layout.findViewById(R.id.safe);
        TextView quality = (TextView) layout.findViewById(R.id.quality);
        TextView progress = (TextView) layout.findViewById(R.id.progress);
        close.setOnClickListener(onClickListener);
        safeImg.setOnClickListener(onClickListener);
        qualityImg.setOnClickListener(onClickListener);
        progressImg.setOnClickListener(onClickListener);
        safe.setOnClickListener(onClickListener);
        quality.setOnClickListener(onClickListener);
        progress.setOnClickListener(onClickListener);
        fullScreenDialog.show();
        fullScreenDialog.setContentView(layout);
    }

    private void initPopupWindow() {
        final View shade = findViewById(R.id.shade);
        View parentView = LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
        View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window_menu, null);
        initEvent(contentView);
        initProjectListView(contentView);
        window = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        window.setOutsideTouchable(true);
        window.setAnimationStyle(R.style.PopupWindowAnimStyle);
        window.showAtLocation(parentView, Gravity.LEFT, 0, 0);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                shade.setVisibility(View.GONE);
            }
        });
        shade.setVisibility(View.VISIBLE);
    }

    private void initProjectListView(View contentView) {
        ListView projectListView = (ListView) contentView.findViewById(R.id.project_list);
        ProjectListAdapter projectListAdapter = new ProjectListAdapter(this, TestData.getProjectData());
        projectListView.setAdapter(projectListAdapter);
    }

    private void initEvent(View contentView) {
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.edit_img:
                    case R.id.edit:
                        window.dismiss();
                        startActivity(new Intent(MainActivity.this, MemberDetailsActivity.class));
                        break;
                    case R.id.settings_img:
                    case R.id.settings:
                        window.dismiss();
                        startActivity(new Intent(MainActivity.this, SettingsActivity.class));
                        break;
                    case R.id.exit_img:
                    case R.id.exit:
                        finish();
                        break;
                }
            }
        };
        ImageView editImg = (ImageView) contentView.findViewById(R.id.edit_img);
        TextView edit = (TextView) contentView.findViewById(R.id.edit);
        ImageView settingsImg = (ImageView) contentView.findViewById(R.id.settings_img);
        TextView settings = (TextView) contentView.findViewById(R.id.settings);
        ImageView exitImg = (ImageView) contentView.findViewById(R.id.exit_img);
        TextView exit = (TextView) contentView.findViewById(R.id.exit);
        editImg.setOnClickListener(onClickListener);
        edit.setOnClickListener(onClickListener);
        settingsImg.setOnClickListener(onClickListener);
        settings.setOnClickListener(onClickListener);
        exitImg.setOnClickListener(onClickListener);
        exit.setOnClickListener(onClickListener);
    }
}
